package com.twl.app.base.ctrl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;

import com.twl.app.base.entity.Companyb;
import com.twl.app.base.entity.Personalmoney;
import com.twl.app.base.entity.Secureunit;
import com.twl.app.base.util.BeanFactoryHolder;
import com.twl.app.base.util.CryptUtils;
import com.twl.core.framework.dao.WSHibernateDaoSupportProxy;

public class GetSalarySum<T> implements Callable<Map<String, T>> {
	
	public Logger log = Logger.getLogger(GetSalarySum.class);

	private Class<T> clazz;
	private String hql;

	private CountDownLatch countDownLatch;

	public GetSalarySum(Class<T> clazz, String hql, CountDownLatch countDownLatch) {
		this.clazz = clazz;
		this.hql = hql;
		this.countDownLatch = countDownLatch;
	}

	@Override
	public Map<String, T> call() throws Exception {
		WSHibernateDaoSupportProxy wSHibernateDaoSupportProxy = WSHibernateDaoSupportProxy.class.cast(BeanFactoryHolder.getBean("wSHibernateDaoSupportProxy"));
		Session session = wSHibernateDaoSupportProxy.getSessionFactory().openSession();

		Map<String, T> map = new HashMap<String, T>();

		try {
			log.info("..." + clazz.getName() + "查询...");
			Query query = session.createQuery(hql.toString());
			@SuppressWarnings("unchecked")
			List<T> list = (List<T>)query.list();
			//解密
			try{
				list = CryptUtils.decryptNew(list);
			}catch(Exception e){
				e.printStackTrace();
			}
			log.info("..." + clazz.getName() + "查询完毕...");

			if (clazz.newInstance() instanceof Companyb) {
				//#####################反射########################
//				Method m = clazz.getDeclaredMethod("getNature");
//				for (T t : list) {
//					// 利用反射获取工作地点
//					String nature = m.invoke(t).toString();
//					map.put(nature, t);
//				}
				//#############################################
				//*******************内省*******************************
				PropertyDescriptor p = new PropertyDescriptor("nature", clazz);
				for (T t : list) {
					// 利用反射获取工作地点
					String nature = p.getReadMethod().invoke(t).toString();
					map.put(nature, t);
				}
				//******************************************************

			} else {
				//###############################反射########################################
//				Class<?> c = null;
//				if (clazz.newInstance() instanceof Dispatchmanger || clazz.newInstance() instanceof Tradecost || clazz.newInstance() instanceof Dutyback || clazz.newInstance() instanceof Personalduty || clazz.newInstance() instanceof Solidmoney || clazz.newInstance() instanceof Dutymoney || clazz.newInstance() instanceof MinMoney || clazz.newInstance() instanceof Personalmoney || clazz.newInstance() instanceof Secureunit || clazz.newInstance() instanceof Dutysendmoney) {
//					c = Information.class;
//				} else {
//					c = clazz;
//				}
//				// 利用反射获取工号
//				Method m = c.getDeclaredMethod("getStaffint");
//
//				if (clazz.newInstance() instanceof Secureunit) {
//					Method m2 = clazz.getDeclaredMethod("getJudgeint");
//					for (T t : list) {
//						String staffint = m.invoke(t).toString();
//						// 利用反射获取类型
//						String type = m2.invoke(t).toString();
//						map.put(staffint + type, t);
//					}
//				} else if (clazz.newInstance() instanceof Personalmoney) {
//					Method m2 = clazz.getDeclaredMethod("getPersontype");
//					for (T t : list) {
//						String staffint = m.invoke(t).toString();
//						// 利用反射获取类型
//						String type = m2.invoke(t).toString();
//						map.put(staffint + type, t);
//					}
//				} else {
//					for (T t : list) {
//						String staffint = m.invoke(t).toString();
//						map.put(staffint, t);
//					}
//				}
				//###########################################################################
				
				//********************************内省*******************************************
				PropertyDescriptor p = new PropertyDescriptor("staffint", clazz);
				Method m = p.getReadMethod();

				if (clazz.newInstance() instanceof Secureunit) {
					PropertyDescriptor p2 = new PropertyDescriptor("judgeint", clazz);
					Method m2 = p2.getReadMethod();
					for (T t : list) {
						String staffint = m.invoke(t).toString();
						// 利用反射获取类型
						String type = m2.invoke(t).toString();
						map.put(staffint + type, t);
					}
				} else if (clazz.newInstance() instanceof Personalmoney) {

					PropertyDescriptor p2 = new PropertyDescriptor("persontype", clazz);
					Method m2 = p2.getReadMethod();
					for (T t : list) {
						String staffint = m.invoke(t).toString();
						// 利用反射获取类型
						String type = m2.invoke(t).toString();
						map.put(staffint + type, t);
					}
				} else {
					for (T t : list) {
						String staffint = m.invoke(t).toString();
						map.put(staffint, t);
					}
				}
				//******************************************************************************
				
			}

			log.info("..." + clazz.getName() + "数据处理完毕...");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null && session.isConnected()) {
				session.close();
				log.info("连接已关闭。。。");
			}
			countDownLatch.countDown();
		}
		return map;
	}
}
